import mongoose from '../config/DBHelpler'
import moment from 'moment'

const Schema = mongoose.Schema

const SignRecordSchema = new mongoose.Schema({
  uid: { type: String, ref: 'users' },
  created: { type: Date },
  favs: { type: Number },
})

SignRecordSchema.pre('save', function (next) {
  this.created = moment().format('YYYY-MM-DD HH:mm:ss')
  next()
})
SignRecordSchema.statics = {
  findByUid: function (uid) {
    return this.findOne({ uid: uid }).sort({ created: -1 })
  },
  findTime: function (opt, limit) {
    return this.find({ created: { $gt: opt } })
      .limit(parseInt(limit))
      .sort({ created: -1 })
      .populate({
        path: 'uid',
        select: 'count name pic',
      })
  },
}

const SignRecordSchemaModel = mongoose.model('sign_record', SignRecordSchema)

export default SignRecordSchemaModel
